//
//  WDRefreshControl.m
//  PullDownRef
//
//  Created by 代辉 on 2017/9/19.
//  Copyright © 2017年 代辉. All rights reserved.
//

#import "WDRefreshControl.h"

typedef NS_ENUM(NSUInteger, RefreshStatus) {
    RefreshStatusNormal,
    RefreshStatusPulling,
    RefreshStatusRefreshing
};

#define selfHeight 35
@interface WDRefreshControl ()

@property (nonatomic,assign) RefreshStatus status;
@property (nonatomic,assign) RefreshStatus currentStatus;
@property (nonatomic,strong) UIImageView *pullDownView;
@property (nonatomic,strong) UIScrollView *currentView;
@property (nonatomic,strong) NSMutableArray *images;

@end
@implementation WDRefreshControl

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
        
    }
    return self;
}
- (NSMutableArray *)images
{
    if (_images == nil) {
        _images = [NSMutableArray new];
        
        for (int i = 19; i < 24; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"图层%zd",i]];
            [_images addObject:image];
        }
    }
    return _images;
}
- (UIImageView *)pullDownView
{
    if (_pullDownView == nil) {
        _pullDownView = [[UIImageView alloc] init];
        _pullDownView.image = self.images.firstObject;
        _pullDownView.animationImages = self.images;
        _pullDownView.animationDuration = self.images.count * 0.15 *SCAL;
        _pullDownView.animationRepeatCount = NSIntegerMax;
    }
    return _pullDownView;
}
- (void)setupUI
{
    self.status = RefreshStatusNormal;
//    self.currentStatus = RefreshStatusRefreshing;
    [self addSubview:self.pullDownView];
    [self.pullDownView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self).offset(-13 *SCAL);
    }];
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if ([newSuperview isKindOfClass:[UIScrollView class]]) {
    
        UIScrollView *currentView = (UIScrollView *)newSuperview;
        [currentView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
        self.frame = CGRectMake(0, -selfHeight *SCAL - 208 *SCAL , DH_Screen_Width, selfHeight *SCAL);
        _currentView = currentView;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentOffset"]) {
        
        CGFloat currentOffent = _currentView.contentOffset.y;
        if (_currentView.isDragging) {
            
            CGFloat limitMaxY = -(_currentView.contentInset.top + selfHeight *SCAL);
            if (currentOffent < limitMaxY && self.status == RefreshStatusNormal) {
                self.status = RefreshStatusPulling;
                self.currentStatus = RefreshStatusNormal;
            } else if (currentOffent >= limitMaxY && self.status == RefreshStatusPulling) {
                self.status = RefreshStatusNormal;
            }
        } else {
            if (self.status == RefreshStatusPulling) {
                self.status = RefreshStatusRefreshing;
            }
        }
    }
}

- (void)setStatus:(RefreshStatus)status
{
    _status = status;
    switch (status) {
        case RefreshStatusNormal:
        {
            if (self.pullDownView.isAnimating) {
                [self.pullDownView stopAnimating];
            }
            [UIView animateWithDuration:0.25 animations:^{
                _currentView.contentInset = UIEdgeInsetsMake(_currentView.contentInset.top - selfHeight *SCAL, _currentView.contentInset.left, _currentView.contentInset.bottom, _currentView.contentInset.right);
            } completion:nil];
        }
            break;
        case RefreshStatusPulling:
            break;
        case RefreshStatusRefreshing:
        {
            [self.pullDownView startAnimating];
            [UIView animateWithDuration:0.25 animations:^{
                
                _currentView.contentInset = UIEdgeInsetsMake(_currentView.contentInset.top + selfHeight *SCAL, _currentView.contentInset.left, _currentView.contentInset.bottom, _currentView.contentInset.right);
            }];
        }
            [self sendActionsForControlEvents:UIControlEventValueChanged];
            break;
    }
}

- (void)endRefreshing
{
    self.status = RefreshStatusNormal;
}
@end
